package com.webchat.chat.web.auth;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.model.User;
import com.webchat.chat.service.UserService;
import com.webchat.chat.web.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by admin on 2018-06-29.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    /**
     * 添加新用户
     * @param user 用户信息(用户名,密码,账号类型必填)
     * @param roleIds 角色ID(必填)
     * @return
     */
    @PostMapping("/add")
    public Result add(User user, String[] roleIds) {
        if(roleIds.length ==0){
            return ResultGenerator.genFailResult("请选择用户角色");
        }
        Result result= userService.checkUser(user);
        if (result.getCode()==400){
            return ResultGenerator.genFailResult("用户名和密码必填,账号类型必填");
        }
        user.setTenantCode(ShiroUtil.getTenantCode());
        userService.addUser(user, roleIds);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        userService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(User user) {
        userService.updateUser(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<User> list = userService.findAllByTenantCode();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String userId) {
        User user = userService.findDetailById(userId);
        return ResultGenerator.genSuccessResult(user);
    }

    @PostMapping("/updateUserAndRole")
    public Result updateUserAndRole(User user ,String[] roleIds) {
        userService.updateUserAndRole(user,roleIds);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/findUserByInfo")
    public Result findUserByInfo(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,User user){
        PageHelper.startPage(page, size);
        if(!"All".equals(ShiroUtil.getTenantCode())){
            user.setTenantCode(ShiroUtil.getTenantCode());
        }
        List<User> userList =userService.findUserByInfo(user);
        PageInfo pageInfo = new PageInfo(userList);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
